<template>
	<view>
		<view class="search-box">
			<view style="display: flex; padding: 15rpx;">
				<!-- #ifdef APP-PLUS -->
				<u-search placeholder="(手机)传票号" search-icon="scan" v-model="dataForm.citationNumber" height="72"
					:show-action="false" @focus="focusCitationNumber('APP')">
				</u-search>
				<u-search placeholder="(PDA)传票号" search-icon="scan" v-model="dataForm.citationNumber" height="72"
					:show-action="false" @focus="focusCitationNumber('PDA')">
				</u-search>
				<!-- #endif -->
				<!-- #ifdef H5 -->
				<u-search placeholder="请扫传票号" search-icon="scan" v-model="dataForm.citationNumber" height="72"
					@search="focusCitationNumber('H5')">
				</u-search>
				<!-- #endif -->


				<u-search placeholder="请选择工艺" v-model="dataForm.citationName" height="72" disabled :show-action="false">
				</u-search>
			</view>
		</view>
		<uni-table border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th align="center">序号</uni-th>
				<uni-th align="center">传票号</uni-th>
				<uni-th align="center">工单单别</uni-th>
				<uni-th align="center">工单单号</uni-th>
				<uni-th align="center">工作中心/供应商编号</uni-th>
				<uni-th align="center">投产批号</uni-th>
				<uni-th align="center">批次数量</uni-th>
				<uni-th align="center">应收数量</uni-th>
				<uni-th width="204" align="center">设置</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="(item,i) in list" :key="i">
				<uni-td align="center">{{i+1}}</uni-td>
				<uni-td align="center">{{item.ub001}}</uni-td>
				<uni-td align="center">{{item.citationNumber}}</uni-td>
				<uni-td align="center">{{item.ub002}}</uni-td>
				<uni-td align="center">{{item.ub006}}</uni-td>
				<uni-td align="center">{{item.ub200}}</uni-td>
				<uni-td align="center">{{item.ua007}}</uni-td>
				<uni-td align="center">{{item.ub205}}</uni-td>
				<uni-td align="center">
					<view class="uni-group">
						<button class="uni-button" size="mini" type="warn" @click="handleDel(i)">删除</button>
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>

		<view class="flowBefore-actions">
			<u-button class="buttom-btn" type="primary" @click.stop="selfSubmit">
				提交
			</u-button>
		</view>
		<u-toast ref="uToast" />
		<u-modal v-model="comShow" :show-cancel-button="true" :show-confirm-button="false" title=''>
			<u-form :errorType="['toast']" label-position="left" label-align="left">
				<u-form-item label="传票号">
					<u-input v-model="comNumber" placeholder="请扫描传票号" :focus="true"></u-input>
				</u-form-item>
			</u-form>
		</u-modal>
	</view>
</template>

<script>
	import request from "@/utils/request";
	export default {
		data() {
			return {
				dataForm: {
					citationNumber: null,
					citationName: null,
					craftNumber: null,
				},
				list: [],
				comNumber: null,
				comShow: false
			}
		},
		watch: {
			comNumber: {
				handler(newVal, oldVal) {
					if (newVal) {
						setTimeout(() => {
							this.dataForm.citationNumber = newVal;
							this.keyupHandle()
							this.comShow = false
						}, 500)
					}
				},
				deep: true,
			},
		},
		onLoad(option) {
			this.dataForm.craftNumber = option.ub004;
			this.dataForm.citationName = option.ub004Name;
		},
		methods: {
			handleDel(index) {
				this.list.splice(index, 1);
			},
			focusCitationNumber(type) {
				if (type === 'APP') {
					uni.scanCode({
						success: (res) => {
							this.dataForm.citationNumber = res.result;
							this.keyupHandle()
						}
					});
				} else if (type === 'PDA') {
					this.comNumber = null;
					this.comShow = true
				} else {
					this.keyupHandle()
				}
			},
			//传票号回车触发方法
			keyupHandle() {
				//判断传票号是否已存在列表
				var index = this.list.findIndex(
					(it) => it.citationNumber == this.dataForm.citationNumber
				);
				if (index !== -1) {
					this.dataForm.citationNumber = null;
					return this.$refs.uToast.show({
						title: `传票号${ this.dataForm.citationNumber}已存在下表，请勿重复添加！`,
						type: "error",
					});
				}
				var query = {
					processNumber: this.dataForm.craftNumber, //工艺编号
					citationNumber: this.dataForm.citationNumber, //传票号
				};
				request({
					url: `/api/SubDev/Sfcub/getInfoByTicketNumberAndProcess`,
					method: "get",
					data: query,
				}).then((res) => {
					if (res.code == 200) {
						var data = res.data;
						var item = {
							citationNumber: data.citationNumber,
							ub001: data.ub001,
							ub002: data.ub002,
							ub003: data.ub003,
							ub006: data.ub006,
							ub200: data.ub200,
							ua007: data.ua007,
							ub010: data.ub010,
							ub205: data.ub010, //接收数量
						};
						this.list.push(item);
						//清空传票号
						this.dataForm.citationNumber = null;
					}
				});
			},

			//项目计划验证
			verify() {
				let isOk = true;
				for (let i = 0; i < this.list.length; i++) {
					const e = this.list[i];
					if (this.jnpf.isEmpty(e.ub205)) {
						return this.$refs.uToast.show({
							title: `传票号${ e.citationNumber}接收数量不能为空!`,
							type: "error",
						});
						isOk = false;
						break;
					}
				}
				return isOk;
			},
			selfSubmit() {
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前数据吗？',
					success: res => {
						if (res.confirm) {
							this.handleConfirm()
						}
					}
				})
			},

			//确 认
			handleConfirm() {
				if (this.verify && !this.verify()) return;
				request({
						url: `/api/SubDev/Sfcub/UpdateReceiveQuantity`,
						method: "put",
						data: this.list,
					})
					.then((res) => {
						uni.showToast({
							title: res.msg,
							complete: () => {
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							}
						})
					})

			},
		}
	}
</script>